<?php

namespace app\common\command;

use app\common\model\Gift;
use app\common\model\GiveLog;
use app\common\model\User;
use think\console\Command;
use think\console\Input;
use think\console\Output;

/**
 * 自动送礼 每日00:00执行
 * Class ClearTask
 * @package app\admin\command
 */
class AutoGive extends Command
{
    protected $model = null;

    protected function configure()
    {
        $this->setName('autoGive')->setDescription('自动送礼');
    }
    protected function execute(Input $input, Output $output)
    {
        $list = GiveLog::where('status',0)->select();
        foreach($list as $v){
            $giftInfo = Gift::where('id',$v['gift_id'])->find();
            if(empty($giftInfo)){
                continue;
            }
            if($giftInfo['types'] == 1){
                $field = 'birthday';
            }else{
                $field = 'die_time';
            }
            $userInfo = User::where('id',$v['to_user_id'])->field('id,birthday,die_time')->find();
            $a = substr($userInfo[$field],5,2);
            $b = substr($userInfo[$field],8,10);
            $a1 = date('m');
            $b1 = date('d');
            if($a == $a1 && $b == $b1){
                User::setMoney($v['user_id'],'score',$v['score'],2020,'赠送'.$v['name'].'获得',$v['user_id'].time());
                User::setMoney($v['to_user_id'],'money',$v['to_money'],2030,'收到'.$v['name'].'获得');
                GiveLog::where('id',$v['id'])->update([
                    'status'=>1
                ]);
            }
        }
        $output->info('完成');
    }
}